package com.weiyi.mashangzhu.Roomer.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Landlord.Activity.MainActivity_L;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LogOutDialog extends Activity {

    String type;    // 消息类型
    static double kLat;
    String data;
    @BindView(R.id.banyuan)
    ImageView banyuan;
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.info)
    TextView info;
    @BindView(R.id.cancal)
    TextView cancal;
    @BindView(R.id.refirm)
    TextView refirm;
    @BindView(R.id.to_landlord)
    RelativeLayout toLandlord;
    @BindView(R.id.close_room)
    ImageView closeRoom;
    @BindView(R.id.title_room)
    TextView titleRoom;
    @BindView(R.id.info_room)
    TextView infoRoom;
    @BindView(R.id.cancal_room)
    TextView cancalRoom;
    @BindView(R.id.refirm_room)
    TextView refirmRoom;
    @BindView(R.id.to_room_yes)
    RelativeLayout toRoom;
    @BindView(R.id.house)
    TextView house;
    @BindView(R.id.date)
    TextView date;
    @BindView(R.id.close_room_no)
    ImageView closeRoomNo;
    @BindView(R.id.title_room_no)
    TextView titleRoomNo;
    @BindView(R.id.info_room_no)
    TextView infoRoomNo;
    @BindView(R.id.refirm_room_no)
    TextView refirmRoomNo;
    @BindView(R.id.to_room_no)
    RelativeLayout toRoomNo;
    @BindView(R.id.cancel_close)
    ImageView cancelClose;
    @BindView(R.id.cancel_title)
    TextView cancelTitle;
    @BindView(R.id.cancel_house)
    TextView cancelHouse;
    @BindView(R.id.cancel_date)
    TextView cancelDate;
    @BindView(R.id.cancel_info)
    TextView cancelInfo;
    @BindView(R.id.cancel_refirm)
    TextView cancelRefirm;
    @BindView(R.id.cancel_to_room)
    RelativeLayout cancelToRoom;
    private MediaPlayer mMediaPlayer = null;//媒体播放器
    private AudioManager mAudioManager = null;//声音管理器
    public SharedPreferences share;
    private static double Lat;
    String id, status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_getui);
        ButterKnife.bind(this);
        MyApp.getInstance().addActivity(this);
        share = getSharedPreferences("usersId", MODE_PRIVATE);
        if (SPUtils.get(this, "vibrato", "0").equals("0")) {
            //不震动
            Log.e("uuuuuuuuuuuuuuuu","不震动");
        } else {
            //震动
            Log.e("uuuuuuuuuuuuuuuu","震动");
            Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
            getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(new long[]{1000,50,50,100,50}, -1);
//            vibrator.cancel();
        }
        getData();
    }

    private void getData() {
        Bundle b = getIntent().getExtras();
        final String info = b.getString("data");
        Log.e("loooooooo", "re=" + info);
        if (info != null) {
            try {
                JSONObject jsonObject = new JSONObject(info);
                id = jsonObject.getString("id");
                if (jsonObject.getString("tuisongPeople").equals("0")) {
                    if (jsonObject.getString("operate").equals("2")) {
                        //房客取消订单
                        toLandlord.setVisibility(View.GONE);
                        toRoomNo.setVisibility(View.GONE);
                        toRoom.setVisibility(View.GONE);
                        cancelToRoom.setVisibility(View.VISIBLE);
                        cancelTitle.setText("房客取消了此订单");
                        cancelHouse.setText(jsonObject.getString("hTitle"));
                        cancelDate.setText(jsonObject.getString("oLivetime") + "~" + jsonObject.getString("oCheckouttime"));
                        cancelClose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LogOutDialog.this.finish();
                            }
                        });
                        cancelRefirm.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LogOutDialog.this.finish();
                            }
                        });

                    } else {
                        //房客下单  推送给房东
                        toLandlord.setVisibility(View.VISIBLE);
                        toRoom.setVisibility(View.GONE);
                        toRoomNo.setVisibility(View.GONE);
                        cancelToRoom.setVisibility(View.GONE);
                        house.setText(jsonObject.getString("hTitle"));
                        date.setText(jsonObject.getString("oLivetime") + "~" + jsonObject.getString("oCheckouttime"));
                        close.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LogOutDialog.this.finish();
                            }
                        });
                        refirm.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Log.e("loooooooo", "re=" + SPUtils.get(LogOutDialog.this, "token", "") + "");
                                Log.e("loooooooo", "re=" + id);
                                //接单
                                OkHttpUtils.get()
                                        .addParams("token", SPUtils.get(LogOutDialog.this, "token", "") + "")
                                        .addParams("id", id)
                                        .addParams("operate", "1")
                                        .url(API.BaseApi_L + API.ReceiveOrder)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {
                                                T.showShort(LogOutDialog.this, "数据加载失败，请稍后再试");
                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("loooooooo", "re=" + response);
                                                if (response != null) {
                                                    try {
                                                        JSONObject jsonObject1 = new JSONObject(response);
                                                        if (jsonObject1.getInt("status") == 0) {
                                                            T.showShort(LogOutDialog.this, "成功接受此订单");
                                                            Intent intent = new Intent(LogOutDialog.this, MainActivity_L.class);
//                                                        intent.putExtra("info",1);
                                                            LogOutDialog.this.startActivity(intent);
                                                            LogOutDialog.this.finish();
                                                        } else {
                                                            T.showShort(LogOutDialog.this, "接受此订单失败");
                                                        }
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                }

                                            }
                                        });
                            }
                        });
                        cancal.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //拒单
                                OkHttpUtils.get()
                                        .addParams("token", SPUtils.get(LogOutDialog.this, "token", "") + "")
                                        .addParams("id", id)
                                        .addParams("operate", "0")
                                        .url(API.BaseApi_L + API.ReceiveOrder)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {
                                                T.showShort(LogOutDialog.this, "数据加载失败，请稍后再试");
                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("loooooooo", "re=" + response);
                                                if (response != null) {
                                                    try {
                                                        JSONObject jsonObject1 = new JSONObject(response);
                                                        if (jsonObject1.getInt("status") == 0) {
                                                            T.showShort(LogOutDialog.this, "成功拒绝此订单");
                                                            LogOutDialog.this.finish();
                                                        } else {
                                                            T.showShort(LogOutDialog.this, "拒绝此订单失败");
                                                        }
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            }
                                        });

                            }
                        });
                        close.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LogOutDialog.this.finish();
                            }
                        });

                    }


                } else if (jsonObject.getString("tuisongPeople").equals("1") && jsonObject.getString("operate").equals("1")) {
                    //房东接单
                    toLandlord.setVisibility(View.GONE);
                    toRoomNo.setVisibility(View.GONE);
                    toRoom.setVisibility(View.VISIBLE);
                    cancelToRoom.setVisibility(View.GONE);
                    refirmRoom.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(LogOutDialog.this, MainActivity_R.class);
                            intent.putExtra("info", 1);
                            LogOutDialog.this.startActivity(intent);
                            LogOutDialog.this.finish();
                        }
                    });
                    cancalRoom.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogOutDialog.this.finish();

                        }
                    });
                    closeRoom.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogOutDialog.this.finish();
                        }
                    });

                } else if (jsonObject.getString("tuisongPeople").equals("1") && jsonObject.getString("operate").equals("2")) {
                    //房东取消订单
                    toLandlord.setVisibility(View.GONE);
                    toRoomNo.setVisibility(View.GONE);
                    toRoom.setVisibility(View.GONE);
                    cancelToRoom.setVisibility(View.VISIBLE);
                    cancelTitle.setText("房东取消了此订单");
                    cancelHouse.setText(jsonObject.getString("hTitle"));
                    cancelDate.setText(jsonObject.getString("oLivetime") + "~" + jsonObject.getString("oCheckouttime"));
                    cancelClose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogOutDialog.this.finish();
                        }
                    });
                    cancelRefirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogOutDialog.this.finish();
                        }
                    });
                } else if (jsonObject.getString("tuisongPeople").equals("1") && jsonObject.getString("operate").equals("0")) {
                    //房东拒单
                    toLandlord.setVisibility(View.GONE);
                    toRoomNo.setVisibility(View.VISIBLE);
                    toRoom.setVisibility(View.GONE);
                    cancelToRoom.setVisibility(View.GONE);
                    refirmRoomNo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogOutDialog.this.finish();
                        }
                    });
                    closeRoomNo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogOutDialog.this.finish();

                        }
                    });

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

//    private void Player() {
//        //播放声音文件
//        //播放声音文件
//        mMediaPlayer = MediaPlayer.create(this, R.raw.news);//加载res/raw的happyis.mp3文件
//        mAudioManager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
//        mMediaPlayer.start();
//    }

    /**
     * 根据经纬度获取距离
     */
    private static final double EARTH_RADIUS = 6378137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double GetDistance(double lng1, double lat1, double lng2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(kLat)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    @OnClick({R.id.cancal, R.id.refirm, R.id.cancal_room, R.id.refirm_room})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancal:
                OkHttpUtils.get()
                        .addParams("token", SPUtils.get(LogOutDialog.this, "token", "") + "")
                        .addParams("id", id)
                        .url(API.BaseApi_L + API.ReceiveOrder)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {

                            }

                            @Override
                            public void onResponse(String response) {

                            }
                        });
                break;
            case R.id.refirm:
                OkHttpUtils.get()
                        .addParams("token", SPUtils.get(LogOutDialog.this, "token", "") + "")
                        .addParams("id", id)
                        .url(API.BaseApi_L + API.ReceiveOrder)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {

                            }

                            @Override
                            public void onResponse(String response) {

                            }
                        });
                break;
            case R.id.cancal_room:
                break;
            case R.id.refirm_room:
                break;
        }
    }
}
